package com.whw.fy.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 发送微信订阅消息工具类
 */
public class SendMessageUtil {



    //续费提醒
    public static String sendRenewalReminderMessage(String studentName,String lessonName,String teacherName,int remainingTime,String openId,String accessToken){

        String tempId="oAvI-KkCHuS3anboGHU7yWU0F4nM2LsOt2fEPkSnkA4";

        String page="";

        Map<String,Map> sendData = new HashMap<>();

        Map<String, String> studentNameData = new HashMap();
        studentNameData.put("value",studentName);
        sendData.put("thing1",studentNameData);

        Map<String, String> LessonData = new HashMap();
        LessonData.put("value",lessonName);
        sendData.put("thing2",LessonData);

        Map<String, String> TeacherNameData = new HashMap();
        TeacherNameData.put("value",teacherName);
        sendData.put("thing3",TeacherNameData);

        Map<String, Integer> remainingTimeData = new HashMap();
        remainingTimeData.put("value",remainingTime);
        sendData.put("number4",remainingTimeData);

        return sendMessage(accessToken, openId, tempId, page, sendData);
    }

    private static String sendMessage(String accessToken,String touser,String templateId,String page,Map data)
    {
        Map param=new HashMap();
        param.put("access_token",accessToken);
        param.put("touser",touser);
        param.put("template_id",templateId);

        if (page!=null)
            if (page.trim()!="")
                param.put("page",page);

        param.put("data",data);

        System.out.println(JSONUtil.parseObj(param).toString());

        String res= HttpRequest.post("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+accessToken)
                .body(JSONUtil.parseObj(param).toString()) // 必须使用RestFul的方式进行请求，不然报错 47001！！！！
                .execute()
                .body();
        return res;
    }
}
